package com.glory.brepda.fragment;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.activity.SettingActivity;
import com.glory.brepda.activity.mfg.CommonlyEndActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.AuthorityEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_TabIndexGridAdapter;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import static com.glory.brepda.utils.MyUtils.setPicToDrawable;

public class MainFragment extends FreeUI_BaseFragment {
    @BindView(R.id.tvTtitle)
    TextView tvTitle;
    @BindView(R.id.ivSetting)
    ImageView ivSetting;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rvTechnicalServiceList)
    RecyclerView rvTechnicalServiceList;
    @BindView(R.id.rvCheckList)
    RecyclerView rvCheckList;
    @BindView(R.id.rvRecipeList)
    RecyclerView rvRecipeList;
    @BindView(R.id.rvMFGList)
    RecyclerView rvMFGList;
    @BindView(R.id.llTechnicalServiceLayout)
    LinearLayout llTechnicalServiceLayout;
    @BindView(R.id.llCheckLayout)
    LinearLayout llCheckLayout;
    @BindView(R.id.llRecipeLayout)
    LinearLayout llRecipeLayout;
    @BindView(R.id.llMFGLayout)
    LinearLayout llMFGLayout;
    @BindView(R.id.llLanjainLayout)
    LinearLayout llLanjainLayout;
    @BindView(R.id.rvLanjianList)
    RecyclerView rvLanjianList;
    // ft测试
    @BindView(R.id.llFtTestLayout)
    LinearLayout llFtTestLayout;
    @BindView(R.id.rvFtTestList)
    RecyclerView rvFtTestList;
    // 治具管理大容器以及界面容器
    @BindView(R.id.toolManageLayout)
    LinearLayout toolManageLayout;
    @BindView(R.id.toolManageList)
    RecyclerView toolManageList;
    //物料管理大容器以及界面容器
    @BindView(R.id.materailManageLayout)
    LinearLayout materailManageLayout;
    @BindView(R.id.materailManageList)
    RecyclerView materailManageList;


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {
        tvTitle.setText("主界面");
        rvTechnicalServiceList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvCheckList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvRecipeList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvMFGList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvLanjianList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvFtTestList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        // 主界面注册治具界面容器
        toolManageList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        materailManageList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    }

    @Override
    protected void initData() {
        try {
            initTechnicalServiceArea();
            initCheckArea();
            initRecipeArea();
            initMFGArea();
            initLanjianArea();
            initFtTestArea();
            // 调用方法，加载界面容器内容
            initToolArea();
            initMaterailArea();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initMaterailArea() throws IOException {
        InputStream materailStream = getActivity().getApplicationContext().getResources().getAssets().open("materail.txt");
        String materailJson = FreeApi_StringUtils.getString(materailStream);
        List<FreeUI_TabIndexGridEntity> toolEntity = filteringData(new Gson().fromJson(materailJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (toolEntity.size() <= 0) {
            materailManageLayout.setVisibility(View.GONE);
            return;
        }
        // 调用公共方法产生一个适配器，并将txt文件中定义的 Activity注入到这个适配器中
        FreeUI_TabIndexGridAdapter materailGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), toolEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        materailGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materailGridAdapter.isFirstOnly(true);
        // 将这个适配器注入到容器里面，容器就能显示Entity通过适配器渲染出来的内容
        materailManageList.setAdapter(materailGridAdapter);
    }

    private void initToolArea() throws IOException {
        InputStream toolStream = getActivity().getApplicationContext().getResources().getAssets().open("tool.txt");
        String toolJson = FreeApi_StringUtils.getString(toolStream);
        List<FreeUI_TabIndexGridEntity> toolEntity = filteringData(new Gson().fromJson(toolJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (toolEntity.size() <= 0) {
            toolManageLayout.setVisibility(View.GONE);
            return;
        }
        // 调用公共方法产生一个适配器，并将txt文件中定义的 Activity注入到这个适配器中
        FreeUI_TabIndexGridAdapter toolGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), toolEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        toolGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        toolGridAdapter.isFirstOnly(true);
        // 将这个适配器注入到容器里面，容器就能显示Entity通过适配器渲染出来的内容
        toolManageList.setAdapter(toolGridAdapter);
    }

    private void initRecipeArea() throws IOException {
        InputStream recipeStream = getActivity().getApplicationContext().getResources().getAssets().open("recipe.txt");
        String recipeJson = FreeApi_StringUtils.getString(recipeStream);
        List<FreeUI_TabIndexGridEntity> recipeEntity = filteringData(new Gson().fromJson(recipeJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (recipeEntity.size() <= 0) {
            llRecipeLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter recipeGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), recipeEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        recipeGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recipeGridAdapter.isFirstOnly(true);
        rvRecipeList.setAdapter(recipeGridAdapter);
    }

    private void initCheckArea() throws IOException {
        InputStream checkJsonStream = getActivity().getApplicationContext().getResources().getAssets().open("check.txt");
        String checkJson = FreeApi_StringUtils.getString(checkJsonStream);
        List<FreeUI_TabIndexGridEntity> checkEntity = filteringData(new Gson().fromJson(checkJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (checkEntity.size() <= 0) {
            llCheckLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter checkGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), checkEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        checkGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkGridAdapter.isFirstOnly(true);
        rvCheckList.setAdapter(checkGridAdapter);
    }

    private void initTechnicalServiceArea() throws IOException {
        InputStream technicalServiceStream = getActivity().getApplicationContext().getResources().getAssets().open("technicalService.txt");
        String technicalService = FreeApi_StringUtils.getString(technicalServiceStream);
        List<FreeUI_TabIndexGridEntity> technicalServiceEntity = filteringData(new Gson().fromJson(technicalService, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (technicalServiceEntity.size() <= 0) {
            llTechnicalServiceLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter technicalServiceGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), technicalServiceEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        technicalServiceGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        technicalServiceGridAdapter.isFirstOnly(true);
        rvTechnicalServiceList.setAdapter(technicalServiceGridAdapter);
    }

    private void initLanjianArea() throws IOException {
        InputStream lanjainStream = getActivity().getApplicationContext().getResources().getAssets().open("lanjian.txt");
        String lanjianJson = FreeApi_StringUtils.getString(lanjainStream);
        List<FreeUI_TabIndexGridEntity> lanjianEntity = filteringData(new Gson().fromJson(lanjianJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (lanjianEntity.size() <= 0) {
            llLanjainLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter lanjianGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), lanjianEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        lanjianGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lanjianGridAdapter.isFirstOnly(true);
        rvLanjianList.setAdapter(lanjianGridAdapter);
    }

    private void initFtTestArea() throws IOException {
        InputStream ftTestStream = getActivity().getApplicationContext().getResources().getAssets().open("sotFtTest.txt");
        String ftTestJson = FreeApi_StringUtils.getString(ftTestStream);
        List<FreeUI_TabIndexGridEntity> ftTestEntity = filteringData(new Gson().fromJson(ftTestJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (ftTestEntity.size() <= 0) {
            llFtTestLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter ftTestGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), ftTestEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        ftTestGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        ftTestGridAdapter.isFirstOnly(true);
        rvFtTestList.setAdapter(ftTestGridAdapter);
    }

    private void initMFGArea() throws IOException {
        InputStream mfgJsonStream = getActivity().getApplicationContext().getResources().getAssets().open("mfg.txt");
        String mfgJson = FreeApi_StringUtils.getString(mfgJsonStream);
        List<FreeUI_TabIndexGridEntity> mfgEntity = filteringData(new Gson().fromJson(mfgJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));

//        FreeUI_TabIndexGridEntity data2 = new FreeUI_TabIndexGridEntity();
//        data2.setPicName("materiel_up");
//        data2.setShowPlugin("header");
//        data2.setText("磨划入库");
//        data2.setSkipClass("com.glory.brepda.activity.mfg.GrindingStockActivity");
//        mfgEntity.add(data2);

        if (mfgEntity.size() <= 0) {
            llMFGLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter mfgGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), mfgEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        mfgGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mfgGridAdapter.isFirstOnly(true);
        rvMFGList.setAdapter(mfgGridAdapter);
    }

    private List<FreeUI_TabIndexGridEntity> filteringData(List<FreeUI_TabIndexGridEntity> entity) {
        List<FreeUI_TabIndexGridEntity> mEntity = new ArrayList();
        for (AuthorityEntity authorityEntity : Constant.AUTHORITY_LIST) {
            for (FreeUI_TabIndexGridEntity item : entity) {
                try {
                    Class<?> aClass = Class.forName(item.getSkipClass());
                    String name = aClass.getSimpleName();

                    if (authorityEntity.getNAME().equals(name)){
                        FreeUI_TabIndexGridEntity data = new FreeUI_TabIndexGridEntity();
                        data.setPicName(item.getPicName());
                        data.setShowPlugin(authorityEntity.getLABELZH());
                        data.setText(authorityEntity.getLABELZH());
                        data.setSkipClass(item.getSkipClass());
                        mEntity.add(data);
                        Constant.mEntity.add(data);
                    }
                } catch (Exception e) {
//                    FreeApi_LogUtils.saveErrorMessages("类名解析失败",item.getSkipClass());
                    e.printStackTrace();
                }

            }
        }

        return mEntity;

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @OnClick(R.id.ivSetting)
    public void onViewClicked() {
        FreeApi_Utils.skipWithExtraIntent("header", getActivity(), SettingActivity.class);
    }
}
